package com.lele.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lele.exception.DepartmentException;
import com.lele.mapper.DepartmentMapper;
import com.lele.pojo.Department;
import com.lele.service.IDepartmentService;
import lombok.val;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lele
 * @since 2022-11-26
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Override
    public List<Department> getTreeDepartment() {
        //获取到所有的部门，递归遍历
        List<Department> departments = this.baseMapper.selectList(null);
        List<Department> collect = departments.stream().filter(item -> {
            return item.getParentId() == 0;
        }).map(item -> {
            item.setChildren(getRecursion(item, departments));
            return item;
        }).collect(Collectors.toList());
        return collect;
    }

    private List<Department> getRecursion(Department root, List<Department> departments) {
        //此时root为根节点
        List<Department> collect = departments.stream().filter(item -> {
            //这里需要使用equals来判等
            return item.getParentId().equals(root.getId());
        }).map(item -> {
            item.setChildren(getRecursion(item, departments));
            return item;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void insertDepartment(String name, Integer parentId) {
        Department department = new Department();
        department.setName(name);
        department.setParentId(parentId);
        //获取到父的depPath然后设置值
        Department parentDepartment = this.baseMapper.selectOne(new LambdaQueryWrapper<Department>().eq(Department::getId, parentId));
        //如果父类的isParent是fale，还需要变为true
        if(parentDepartment.getIsParent()==false){
            parentDepartment.setIsParent(true);
        }
        this.baseMapper.updateById(parentDepartment);
        department.setEnabled(true);
        department.setIsParent(false);
        this.baseMapper.insert(department);
        String depPath = parentDepartment.getDepPath();
        department.setDepPath(depPath+"."+department.getId());
        this.baseMapper.updateById(department);
    }

    @Override
    public void deleteDepartment(Integer id) throws DepartmentException {
        /**
         * 删除部门需要查看部门下是否有子部门，如果有子部门不能删除
         */
        Department department = this.baseMapper.selectOne(new LambdaQueryWrapper<Department>().eq(Department::getId, id));
        if(department.getIsParent()){
            throw new DepartmentException();
        }
        //如果当前节点的父节点只有删除节点这一个子节点 isParent也需要变化
        Integer parentId = department.getParentId();
        List<Department> selectList = this.baseMapper.selectList(new LambdaQueryWrapper<Department>().eq(Department::getParentId, parentId));
        if(selectList.size()<=1){
            //需要修改父节点的isParent
            Department parentDepartment = new Department();
            parentDepartment.setIsParent(false);
            parentDepartment.setId(parentId);
            this.baseMapper.updateById(parentDepartment);
        }

        this.baseMapper.deleteById(id);

    }


}
